<?php
/**
 * 抽奖模型
 *
 */
namespace Admin\Model;
use Think\Model;
class PUserModel extends BaseModel{

	private $scoreType = array(1=>"报名活动",2=>"导入",3=>"兑换", 4=>"抽奖", 5=>"分享", 6=>"签到", 7=>"汇总");

	public function getUserList($data, $down = false)
	{
	    $time=time();
		$map['is_deleted'] = 0;
		if (isset($data['phone']) and $data['phone'] != '') {
			$map['phone'] = array('LIKE', '%'.$data['phone'].'%');
		}
		if (isset($data['name']) and $data['name'] != '') {
			$map['name'] = array('LIKE', '%'.$data['name'].'%');
		}
		if (isset($data['account']) and $data['account'] != '') {
			$map['account'] = array('LIKE', '%'.$data['account'].'%');
		}
		if($down){
			$list = $this->where($map)->field('id,name,phone,account,add_time')
					->order('id desc')
			      	->select();

            $users='( ';
            foreach($list as $k =>$v) {
                $users=$users.$v['id'].',';
            }
            $users=$users.'0 )';


            $records=D("PScoreRecord")->query('select user_id, consume from zsf_p_score_record where is_deleted=0 and consume>0 and expire_time>='.$time.' and type in (1,2,5,6,7) and user_id in '.$users);

            foreach($list as $k =>$v){
                $sum=0;
                foreach ($records as $k1 =>$v1){
                    if($v1['user_id']==$v['id']){
                        $sum+=$v1['consume'];
                    }
                }
                $list[$k]['score']=$sum;
            }

			return $list;
		}
		$total = $this->where($map)->count();
		$page = pagination($total, $data['page_size'] ,$data['page']);
		if ($total) {
			$list = $this->where($map)->order('id desc')
			      ->limit($page['offset'], $page['limit'])
			      ->select();


            $users='( ';
            foreach($list as $k =>$v) {
                $users=($users.$v['id'].',');
            }
            $users=$users.'0 )';

            $records=D("PScoreRecord")->query('select user_id, consume from zsf_p_score_record where is_deleted=0 and consume>0 and expire_time>='.$time.' and type in (1,2,5,6,7) and user_id in '.$users);

            foreach ($list as $k => $v) {
				$list[$k]['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
                $sum=0;
                foreach ($records as $k1 =>$v1){
                    if($v1['user_id']==$v['id']){
                        $sum+=$v1['consume'];
                    }
                }
                $list[$k]['score']=$sum;
            }
			return array('data'=>$list, 'page'=>$page);
		}
		return array('data'=>array(),'page'=>$page);
	}


	/**
	 * 获取积分记录
	 * @param  array  $data 过滤参数
	 * @return  
	 */
	public function getRecordList($data = array(), $down = false, $count = 0)
	{
	    $dateSplit0=strtotime("2017-01-01 00:00:00");

	    $dateSplit1=strtotime("2017-12-31 23:59:59");

	    $dateSplit2=strtotime("2018-12-31 23:59:59");

		$model = M('PScoreRecord');
		
		//组装查询条件
		$where['r.is_deleted'] = 0;
		
		//活动名称
		if (isset($data['name']) and $data['name'] != '') {
			$where['u.name'] = array('LIKE', '%'.$data['name'].'%');
		}
		if (isset($data['phone']) and $data['phone'] != '') {
			$where['u.phone'] = array('LIKE', '%'.$data['phone'].'%');
		}
		if (isset($data['account']) and $data['account'] != '') {
			$where['u.account'] = array('LIKE', '%'.$data['account'].'%');
		}
		if (isset($data['operate_type']) and in_array($data['operate_type'], array(1,2))) {
			if ($data['operate_type'] == 1) {
				$where['r.score'] = array('gt', 0);
			}else{
				$where['r.score'] = array('lt', 0);
			}
		}
		if (isset($data['type']) and $data['type'] > 0 and $data['type'] <= 5) {
			$where['type'] = $data['type'];
		}
		if (isset($data['type']) and $data['type'] ==  6) {
			// $where['_query'] =  'type=6&type=7&_logic=or';
			$where['type'] = array('BETWEEN', array(6,7));
		}

		//活动创建时间开始区间
		if (isset($data['start']) and $data['start']) {
			$where['r.add_time'][] = array('egt', strtotime($data['start']));
		}
		//活动创建时间结束区间
		if (isset($data['end']) and $data['end']) {
			$where['r.add_time'][] = array('elt', strtotime($data['end'].' 23:59:59'));
		}

		if($data['timeType']>0){
		    if($data['timeType']==2){      //2018年
		        $where['r.add_time'][] = array('egt', $dateSplit0);

			    $where['r.add_time'][] = array('elt', $dateSplit1);
		    }
		    else{         //下半年
			    $where['r.add_time'][] = array('egt', $dateSplit1);

                $where['r.add_time'][] = array('elt', $dateSplit2);
		    }
		}
		if($count){
			$total = $count;
		}else{
			$model->alias('r')->join('__P_USER__ u ON u.id = r.user_id', 'LEFT');
			$total = $model->where($where)->count();
		}
		$pageIndex = $data['pageIndex'];
		$pageSize = $data['pageSize'];
		$page = pagination($total, $pageSize ,$pageIndex);
		if ($total) {
			if($down){
				$list = $model->alias('r')->field('r.*,b.title,b.prize_name')
						->join('__P_USER__ u ON u.id = r.user_id', 'LEFT')
			      		->where($where)->order('r.id desc')->select();
			}else{
				$list = $model->alias('r')->field('r.*,u.name,u.phone,u.account')
						->join('__P_USER__ u ON u.id = r.user_id', 'LEFT')
				      	->where($where)->order('r.id desc')
				      	->limit($page['offset'], $page['limit'])->select();
			  }
			foreach ($list as $k => $v) {
				$list[$k]['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
                $list[$k]['expire_time'] = date('Y-m-d H:i:s', $v['expire_time']);


				$list[$k]['type'] = $this->scoreType[$v['type']];
				if($v['add_time']>=$dateSplit1 && $v['add_time']<=$dateSplit2){
				    $list[$k]['time_type']='2018年';
				}
				else if($v['add_time']>=$dateSplit0 && $v['add_time']<=$dateSplit1){
		             $list[$k]['time_type']='2017年';
				}
				else{
		             $list[$k]['time_type']='其它';
				}
			}
			return array('data'=>$list, 'page'=>$page);
		}
		return array('data'=>array(),'page'=>$page);

	}

	public function getCount($data)
	{
		$dateSplit0=strtotime("2017-01-01 00:00:00");

	    $dateSplit1=strtotime("2017-06-30 23:59:59");

	    $dateSplit2=strtotime("2017-12-31 23:59:59");

		$model = M('PScoreRecord');
		$model->alias('r')->join('__P_USER__ u ON u.id = r.user_id', 'LEFT');
		//组装查询条件
		$where['r.is_deleted'] = 0;
		
		//活动名称
		if (isset($data['name']) and $data['name'] != '') {
			$where['u.name'] = array('LIKE', '%'.$data['name'].'%');
		}
		if (isset($data['phone']) and $data['phone'] != '') {
			$where['u.phone'] = array('LIKE', '%'.$data['phone'].'%');
		}
		if (isset($data['account']) and $data['account'] != '') {
			$where['u.account'] = array('LIKE', '%'.$data['account'].'%');
		}
		if (isset($data['operate_type']) and in_array($data['operate_type'], array(1,2))) {
			if ($data['operate_type'] == 1) {
				$where['r.score'] = array('gt', 0);
			}else{
				$where['r.score'] = array('lt', 0);
			}
		}
		if (isset($data['type']) and $data['type'] > 0 and $data['type'] <= 5) {
			$where['type'] = $data['type'];
		}
		if (isset($data['type']) and $data['type'] ==  6) {
			// $where['_query'] =  'type=6&type=7&_logic=or';
			$where['type'] = array('BETWEEN', array(6,7));
		}

		//活动创建时间开始区间
		if (isset($data['start']) and $data['start']) {
			$where['r.add_time'][] = array('egt', strtotime($data['start']));
		}
		//活动创建时间结束区间
		if (isset($data['end']) and $data['end']) {
			$where['r.add_time'][] = array('elt', strtotime($data['end'].' 23:59:59'));
		}

		if($data['timeType']>0){
		    if($data['timeType']==1){      //上半年
		        $where['r.add_time'][] = array('egt', $dateSplit0);

			    $where['r.add_time'][] = array('elt', $dateSplit1);
		    }
		    else{         //下半年
			    $where['r.add_time'][] = array('egt', $dateSplit1);

                $where['r.add_time'][] = array('elt', $dateSplit2);
		    }
		}
		return $model->where($where)->count();
	}

}
?>